package com.ruoyi.common.security.exception;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.ruoyi.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class CustomOAuth2ExceptionSerializer extends StdSerializer<CustomOAuth2Exception> {


    public CustomOAuth2ExceptionSerializer() {
        super(CustomOAuth2Exception.class);
    }

    @Override
    public void serialize(CustomOAuth2Exception e, JsonGenerator jGen, SerializerProvider provider) throws IOException {
        log.info("CustomOAuth2ExceptionSerializer返回格式序列化处理，CustomOAuth2ExceptionSerializer.class -> e={}", e);

        // 自定义返回格式内容
        R rt = R.fail();
        rt.setMsg(e.getMessage());
        rt.setCode(e.getOAuth2ErrorCode());
        rt.setData(e.getData());
        jGen.writeObject(rt);
    }
}